﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Vmago.Models;
using Vmago;
using System.Xml;
using System.Collections;
using System.Web.Mvc;
using System.Web.UI;
using Vmago.Models.DBML;
using Vmago.Models.DAL;

namespace Vmago.Controllers
{
    public class UtilController : ControllerBase
    {
        #region Global：Member

        /// <summary>共同方法</summary>
        private ComDAL m_dalCom = new ComDAL();

        #endregion

        #region Ajax：Get：GetProvince

        public ActionResult Ajax_GetProvince(String id)
        {
            return Json(m_dalCom.GetGeoProvince(id).ToList());
        }

        #endregion

        #region Ajax：Get：GetCity

        public ActionResult Ajax_GetCity(String id)
        {
            return Json(m_dalCom.GetGeoCity(id).ToList());
        }

        #endregion

        #region Ajax：Get：GetSuburb

        public ActionResult Ajax_GetSuburb(String id)
        {
            return Json(m_dalCom.GetGeoSuburb(id).ToList());
        }

        #endregion

        #region Html：Get：共通View

       public ActionResult ViewUtil(String ViewName, ControllerBase Control)
        {
            Control.Session.Clear();
            return View(ViewName);
        }

        #endregion 

        /// <summary>查询城市天气情况</summary>
        public void getCityWeather(string id)
        {
            List<object> obj = new List<object>();
            string s = null;
            string date = null;
            string day = null;
            string weather = null;
            string low = null;
            string high = null;
            XmlDocument document = new XmlDocument();
            string url = "http://xml.weather.yahoo.com/forecastrss?p=JAXX";

            string citycode = CityNameToCityNum(id);

            //增加citycode为空的判断，如果citycode==City not found,则返回RenderJSON告诉页面无天气信息

            //citycode不为空，则做如下操作
            if (citycode != "" || citycode != null)
            {
                document.Load(url + citycode);

                XmlNodeList nodes = document.GetElementsByTagName("forecast",
                    @"http://xml.weather.yahoo.com/ns/rss/1.0");

                foreach (XmlNode node in nodes)
                {
                    date = EmonthToCmonth(node.Attributes["date"].InnerText);
                    day = node.Attributes["day"].InnerText;
                    string weekDay = EweekToCweek(day);
                    //翻译天气
                    weather = WeatherTransCh(node.Attributes["code"].InnerText);
                    low = FToC(int.Parse(node.Attributes["low"].InnerText));
                    high = FToC(int.Parse(node.Attributes["high"].InnerText));
                    s = date + "   " + weekDay + "   天气:" + weather + "   温度:" + low + "°C至" + high + "°C";
                    obj.Add(s);
                }
            }
            else
            {

                s = "页面无天气信息!";
                obj.Add(s);
            }

            RenderJSON(obj);
        }

        /// <summary>根据ID得到城市图标</summary>
        public void getCityImg(string id)
        {
            List<object> obj = new List<object>();
            string s = null;
            try
            {
                string imgUrl = CityImg(id);
                if (imgUrl != "no img")
                {
                    s = "/Content/Img/CityFlower/" + imgUrl;

                }
                else
                {
                    s = "no image";
                }
                obj.Add(s);
            }
            catch (Exception ex)
            {
                Console.WriteLine("CityController:changeCity" + ex);
                string o = " | ";
                obj.Add(o);
            }
            RenderJSON(obj);

        }

        /// <summary>转换温度</summary>
        private static string FToC(int f)
        {
            return Math.Round((f - 32) / 1.8, 1).ToString();
        }

        private string EmonthToCmonth(string strReplace)
        {
            return Convert.ToDateTime(strReplace).ToString("yyyy年MM月dd日");
        }

        /// <summary>转换星期</summary>
        private string EweekToCweek(string strEweek)
        {
            switch (strEweek)
            {
                case "Mon":
                    return "星期一";
                case "Tue":
                    return "星期二";
                case "Wed":
                    return "星期三";
                case "Thu":
                    return "星期四";
                case "Fri":
                    return "星期五";
                case "Sat":
                    return "星期六";
                case "Sun":
                    return "星期日";
                default:
                    return "传参错误";
            }
        }

        /// <summary>根据城市ID返回城市图标</summary>
        private string CityImg(string CityID)
        {
            Hashtable htWeather = new Hashtable();
            htWeather.Add("1", "zhahuang.png");      //札幌
            htWeather.Add("2", "xiaozun.gif");      //小樽
            htWeather.Add("3", "zhinei.gif");
            htWeather.Add("4", "");
            htWeather.Add("5", "xunchuan.jpg");      //旭川
            htWeather.Add("6", "wangzou.gif");
            htWeather.Add("7", "chuanlu.jpg");
            htWeather.Add("8", "hanguan.gif");      //函馆
            htWeather.Add("9", "");
            htWeather.Add("10", "");
            htWeather.Add("11", "xinxie.jpg");     //新潟
            htWeather.Add("12", "");     //长野
            htWeather.Add("13", "shanli.gif");     //山梨
            htWeather.Add("14", "");     //富山
            htWeather.Add("15", "");     //石川
            htWeather.Add("16", "fujin.jpg");
            htWeather.Add("17", "jinggang.jpg");     //静冈
            htWeather.Add("18", "aizhi.png");
            htWeather.Add("19", "");     //岐阜
            htWeather.Add("20", "qingsen.gif");     //青森
            htWeather.Add("21", "qiutian.png");     //秋田
            htWeather.Add("22", "yanshou.png");
            htWeather.Add("23", "shanxing.gif");     //山形
            htWeather.Add("24", "gongcheng.png");     //宫城
            htWeather.Add("25", "fudao.jpg");     //福岛
            htWeather.Add("26", "");     //东京
            htWeather.Add("27", "shennaichuang.gif");     //神奈川
            htWeather.Add("28", "qianye.jpg");     //千叶
            htWeather.Add("29", "qiyu.jpg");
            htWeather.Add("30", "cicheng.png");
            htWeather.Add("31", "limu.png");
            htWeather.Add("32", "qunma.png");
            htWeather.Add("33", "daban.png");     //大阪
            htWeather.Add("34", "jingdu.png");     //京都
            htWeather.Add("35", "bingku.png");
            htWeather.Add("36", "nailiang.gif");     //奈良
            htWeather.Add("37", "hegeshan.gif");     //和歌山
            htWeather.Add("38", "sanchong.png");
            htWeather.Add("39", "");
            htWeather.Add("40", "guangdao.gif");     //广岛
            htWeather.Add("41", "shankou.gif");     //山口
            htWeather.Add("42", "gangshan.gif");     //冈山
            htWeather.Add("43", "niaoqu.jpg");
            htWeather.Add("44", "niaogen.png");
            htWeather.Add("45", "");     //香川
            htWeather.Add("46", "dedao.gif");     //德岛
            htWeather.Add("47", "");
            htWeather.Add("48", "gaozhi.png");     //高知
            htWeather.Add("49", "fugang.gif");     //福冈
            htWeather.Add("50", "zuohe.gif");
            htWeather.Add("51", "changqi.gif");     //长崎
            htWeather.Add("52", "xiongben.gif");     //熊本
            htWeather.Add("53", "dafen.jpg");
            htWeather.Add("54", "gongqi.jpg");     //宫崎
            htWeather.Add("55", "luerdao.gif");     //鹿儿岛
            htWeather.Add("56", "naba.gif");     //那霸
            htWeather.Add("57", "chongsheng.jpg");     //冲绳
            htWeather.Add("58", "chongsheng.jpg");     //冲绳
            htWeather.Add("59", "chongsheng.jpg");     //冲绳
            htWeather.Add("60", "henggudao.jpg");     //宫古岛
            htWeather.Add("61", "");
            htWeather.Add("62", "");
            htWeather.Add("63", "");

            object cityNum = htWeather[CityID];

            if (cityNum == null || cityNum.ToString() == "")
            {
                return "no img";
            }
            else
            {
                return cityNum.ToString();
            }
        }

        /// <summary>根据城市名称返回城市编号</summary>
        public void searchCityName(string id)
        {
            List<object> obj = new List<object>();
            string s = null;
            try
            {
                string CityID = getCityNum(id);
                if (CityID != "nothing" || CityID != null)
                {
                    s = CityID;
                }
                else
                {
                    s = "nothing";
                }
                obj.Add(s);
            }
            catch (Exception ex)
            {
                Console.WriteLine("CityController:changeCity" + ex);
                string o = " | ";
                obj.Add(o);
            }
            RenderJSON(obj);

        }

        /// <summary>根据城市名称返回城市编号</summary>
        private string getCityNum(string SearchCityName)
        {
            Hashtable htWeather = new Hashtable();
            htWeather.Add("札幌", "1");      //札幌
            htWeather.Add("小樽", "2");      //小樽
            htWeather.Add("稚内", "nothing");
            htWeather.Add("知床", "nothing");
            htWeather.Add("旭川", "nothing");      //旭川
            htWeather.Add("网走", "6");
            htWeather.Add("钏路", "7");
            htWeather.Add("函馆", "8");      //函馆
            htWeather.Add("利尻岛", "nothing");
            htWeather.Add("礼文岛", "nothing");
            htWeather.Add("新潟", "11");     //新潟
            htWeather.Add("长野", "12");     //长野
            htWeather.Add("山梨", "13");     //山梨
            htWeather.Add("富山", "14");     //富山
            htWeather.Add("石川", "15");     //石川
            htWeather.Add("福井", "16");
            htWeather.Add("静冈", "17");     //静冈
            htWeather.Add("爱知", "18");
            htWeather.Add("岐阜", "19");     //岐阜
            htWeather.Add("青森", "nothing");     //青森
            htWeather.Add("秋田", "21");     //秋田
            htWeather.Add("岩手", "22");
            htWeather.Add("山形", "23");     //山形
            htWeather.Add("宫城", "24");     //宫城
            htWeather.Add("福岛", "25");     //福岛
            htWeather.Add("东京", "26");     //东京
            htWeather.Add("神奈川", "27");     //神奈川
            htWeather.Add("千叶", "28");     //千叶
            htWeather.Add("埼玉", "29");
            htWeather.Add("茨城", "30");
            htWeather.Add("枥木", "31");
            htWeather.Add("群马", "32");
            htWeather.Add("大阪", "33");     //大阪
            htWeather.Add("京都", "34");     //京都
            htWeather.Add("兵库", "35");
            htWeather.Add("奈良", "36");     //奈良
            htWeather.Add("和歌山", "37");     //和歌山
            htWeather.Add("三重", "38");
            htWeather.Add("滋贺", "39");
            htWeather.Add("广岛", "40");     //广岛
            htWeather.Add("山口", "nothing");     //山口
            htWeather.Add("冈山", "42");     //冈山
            htWeather.Add("鸟取", "nothing");
            htWeather.Add("岛根", "nothing");
            htWeather.Add("香川", "45");     //香川
            htWeather.Add("德岛", "nothing");     //德岛
            htWeather.Add("爱媛", "47");
            htWeather.Add("高知", "nothing");     //高知
            htWeather.Add("福冈", "49");     //福冈
            htWeather.Add("佐贺", "nothing");
            htWeather.Add("长崎", "51");     //长崎
            htWeather.Add("熊本", "52");     //熊本
            htWeather.Add("大分", "53");
            htWeather.Add("宫崎", "54");     //宫崎
            htWeather.Add("鹿儿岛", "55");     //鹿儿岛
            htWeather.Add("那霸", "56");     //那霸
            htWeather.Add("冲绳南部", "nothing");     //冲绳
            htWeather.Add("冲绳中部", "58");     //冲绳
            htWeather.Add("冲绳北部", "nothing");     //冲绳
            htWeather.Add("宫古岛", "nothing");     //宫古岛
            htWeather.Add("石垣岛", "nothing");
            htWeather.Add("庆良间诸岛", "nothing");
            htWeather.Add("西表岛", "nothing");

            object cityNum = htWeather[SearchCityName];

            if (cityNum == null)
            {
                return null;
            }
            else
            {
                return cityNum.ToString();
            }
        }

        /// <summary>根据城市名称返回城市编号</summary>
        private string CityNameToCityNum(string CityId)
        {
            //替换成我们目前日本城市的num
            Hashtable htWeather = new Hashtable();
            htWeather.Add("1", "0078");      //札幌
            htWeather.Add("2", "0002");      //小樽
            htWeather.Add("3", "0044");
            htWeather.Add("4", "0005");
            htWeather.Add("5", "0005");      //旭川
            htWeather.Add("6", "0031");
            htWeather.Add("7", "0017");
            htWeather.Add("8", "0014");      //函馆
            htWeather.Add("9", "0049");
            htWeather.Add("10", "0064");
            htWeather.Add("11", "0061");     //新潟
            htWeather.Add("12", "0105");     //长野
            htWeather.Add("13", "0096");     //山梨
            htWeather.Add("14", "0086");     //富山
            htWeather.Add("15", "0021");     //石川
            htWeather.Add("16", "0259");
            htWeather.Add("17", "0107");     //静冈
            htWeather.Add("18", "0080");
            htWeather.Add("19", "0012");     //岐阜
            htWeather.Add("20", "0004");     //青森
            htWeather.Add("21", "0001");     //秋田
            htWeather.Add("22", "0122");
            htWeather.Add("23", "0096");     //山形
            htWeather.Add("24", "0110");     //宫城
            htWeather.Add("25", "0010");     //福岛
            htWeather.Add("26", "0085");     //东京
            htWeather.Add("27", "0030");     //神奈川
            htWeather.Add("28", "0006");     //千叶
            htWeather.Add("29", "0079");
            htWeather.Add("30", "0236");
            htWeather.Add("31", "0099");
            htWeather.Add("32", "0099");
            htWeather.Add("33", "0071");     //大阪
            htWeather.Add("34", "0047");     //京都
            htWeather.Add("35", "0099");
            htWeather.Add("36", "0060");     //奈良
            htWeather.Add("37", "0095");     //和歌山
            htWeather.Add("38", "0099");
            htWeather.Add("39", "0099");
            htWeather.Add("40", "0018");     //广岛
            htWeather.Add("41", "0096");     //山口
            htWeather.Add("42", "0065");     //冈山
            htWeather.Add("43", "0099");
            htWeather.Add("44", "0099");
            htWeather.Add("45", "0030");     //香川
            htWeather.Add("46", "0084");     //德岛
            htWeather.Add("47", "0099");
            htWeather.Add("48", "0040");     //高知
            htWeather.Add("49", "0009");     //福冈
            htWeather.Add("50", "0099");
            htWeather.Add("51", "0055");     //长崎
            htWeather.Add("52", "0043");     //熊本
            htWeather.Add("53", "0099");
            htWeather.Add("54", "0110");     //宫崎
            htWeather.Add("55", "0028");     //鹿儿岛
            htWeather.Add("56", "0058");     //那霸
            htWeather.Add("57", "0067");     //冲绳
            htWeather.Add("58", "0067");     //冲绳
            htWeather.Add("59", "0067");     //冲绳
            htWeather.Add("60", "0111");     //宫古岛
            htWeather.Add("61", "0099");
            htWeather.Add("62", "0099");
            htWeather.Add("63", "0099");

            object cityNum = htWeather[CityId];

            if (cityNum == null)
            {
                return null;
            }
            else
            {
                return cityNum.ToString();
            }
        }

        public string getCityName(string SearchCityID)
        {
            Hashtable htWeather = new Hashtable();
            htWeather.Add("1", "札幌");      //札幌
            htWeather.Add("2", "小樽");      //小樽
            //htWeather.Add("稚内", "nothing");
            //htWeather.Add("知床", "nothing");
            //htWeather.Add("旭川", "nothing");      //旭川
            htWeather.Add("6", "网走");
            htWeather.Add("7", "钏路");
            htWeather.Add("8", "函馆");      //函馆
            // htWeather.Add("利尻岛", "nothing");
            // htWeather.Add("礼文岛", "nothing");
            htWeather.Add("11", "新潟");     //新潟
            htWeather.Add("12", "长野");     //长野
            htWeather.Add("13", "山梨");     //山梨
            htWeather.Add("14", "富山");     //富山
            htWeather.Add("15", "石川");     //石川
            htWeather.Add("16", "福井");
            htWeather.Add("17", "静冈");     //静冈
            htWeather.Add("18", "爱知");
            htWeather.Add("19", "岐阜");     //岐阜
            // htWeather.Add("青森", "nothing");     //青森
            htWeather.Add("21", "秋田");     //秋田
            htWeather.Add("22", "岩手");
            htWeather.Add("23", "山形");     //山形
            htWeather.Add("24", "宫城");     //宫城
            htWeather.Add("25", "福岛");     //福岛
            htWeather.Add("26", "东京");     //东京
            htWeather.Add("27", "神奈川");     //神奈川
            htWeather.Add("28", "千叶");     //千叶
            htWeather.Add("29", "埼玉");
            htWeather.Add("30", "茨城");
            htWeather.Add("31", "枥木");
            htWeather.Add("32", "群马");
            htWeather.Add("33", "大阪");     //大阪
            htWeather.Add("34", "京都");     //京都
            htWeather.Add("35", "兵库");
            htWeather.Add("36", "奈良");     //奈良
            htWeather.Add("37", "和歌山");     //和歌山
            htWeather.Add("38", "三重");
            htWeather.Add("39", "滋贺");
            htWeather.Add("40", "广岛");     //广岛
            //htWeather.Add("山口", "nothing");     //山口
            htWeather.Add("42", "冈山");     //冈山
            // htWeather.Add("鸟取", "nothing");
            // htWeather.Add("岛根", "nothing");
            htWeather.Add("45", "香川");     //香川
            // htWeather.Add("德岛", "nothing");     //德岛
            htWeather.Add("47", "爱媛");
            // htWeather.Add("高知", "nothing");     //高知
            htWeather.Add("49", "福冈");     //福冈
            //htWeather.Add("佐贺", "nothing");
            htWeather.Add("51", "长崎");     //长崎
            htWeather.Add("52", "熊本");     //熊本
            htWeather.Add("53", "大分");
            htWeather.Add("54", "宫崎");     //宫崎
            htWeather.Add("55", "鹿儿岛");     //鹿儿岛
            htWeather.Add("56", "那霸");     //那霸
            // htWeather.Add("冲绳南部", "nothing");     //冲绳
            htWeather.Add("58", "冲绳中部");     //冲绳
            //  htWeather.Add("冲绳北部", "nothing");     //冲绳
            // htWeather.Add("宫古岛", "nothing");     //宫古岛
            // htWeather.Add("石垣岛", "nothing");
            //htWeather.Add("庆良间诸岛", "nothing");
            //htWeather.Add("西表岛", "nothing");

            object cityNum = htWeather[SearchCityID];

            if (cityNum == null)
            {
                return null;
            }
            else
            {
                return cityNum.ToString();
            }
        }

        /// <summary>翻译天气</summary>
        private string WeatherTransCh(string CityId)
        {
            //替换中文的天气情况http://developer.yahoo.com/weather/
            Hashtable htWeather = new Hashtable();
            htWeather.Add("0", "龙卷风"); //"tornado");
            htWeather.Add("1", "热带暴雨"); //tropical storm
            htWeather.Add("2", "飓风"); //hurricane
            htWeather.Add("3", "强大雷暴"); //severe thunderstorms
            htWeather.Add("4", "雷暴"); //thunderstorms
            htWeather.Add("5", "雨夹雪"); //mixed rain and snow
            htWeather.Add("6", "雨夹雪（冻雨）"); //mixed rain and sleet
            htWeather.Add("7", "雨夹雪（冰雹）"); //mixed snow and sleet
            htWeather.Add("8", "冻毛毛雨"); //freezing drizzle
            htWeather.Add("9", "毛毛雨"); //drizzle
            htWeather.Add("10", "冻雨");//freezing rain
            htWeather.Add("11", "暴雨");//showers
            htWeather.Add("12", "暴雨");//showers
            htWeather.Add("13", "小雪");//snow flurries
            htWeather.Add("14", "小雪转阵雨");//light snow showers
            htWeather.Add("15", "高吹雪");//blowing snow
            htWeather.Add("16", "雪");//snow
            htWeather.Add("17", "冰雹");//hail
            htWeather.Add("18", "冰雹");//sleet
            htWeather.Add("19", "尘埃");//dust
            htWeather.Add("20", "雾");//foggy
            htWeather.Add("21", "霾");//haze"
            htWeather.Add("22", "烟");//smoky
            htWeather.Add("23", "狂风暴雨");//blustery
            htWeather.Add("24", "大风");//windy
            htWeather.Add("25", "寒冷");//cold
            htWeather.Add("26", "多云");//cloudy
            htWeather.Add("27", "夜间多云");//mostly cloudy (night)
            htWeather.Add("28", "白天多云");//mostly cloudy (day)
            htWeather.Add("29", "夜间局部多云");//partly cloudy (night)
            htWeather.Add("30", "白天局部多云");//partly cloudy (day)
            htWeather.Add("31", "夜间晴朗");//clear (night)
            htWeather.Add("32", "晴");//sunny
            htWeather.Add("33", "夜间转晴");//fair (night)
            htWeather.Add("34", "白天转晴");//fair (day)
            htWeather.Add("35", "雨夹冰雹");//mixed rain and hail
            htWeather.Add("36", "热");//hot
            htWeather.Add("37", "局部雷雨");//isolated thunderstorms
            htWeather.Add("38", "雷雨");//scattered thunderstorms
            htWeather.Add("39", "局部雷雨");//scattered thunderstorms
            htWeather.Add("40", "分散暴风雨");//scattered showers
            htWeather.Add("41", "大雪");//heavy snow
            htWeather.Add("42", "分散暴风雪");//scattered snow showers
            htWeather.Add("43", "大雪");//heavy snow
            htWeather.Add("44", "局部多云");//partly cloudy
            htWeather.Add("45", "雷阵雨");//thundershowers
            htWeather.Add("46", "暴风雪");//snow showers
            htWeather.Add("47", "局部雷阵雨");//isolated thundershowers
            htWeather.Add("3200", "未知");//not available
            object weatherCondition = htWeather[CityId];

            if (weatherCondition == null)
            {
                return "weather not found";
            }
            else
            {
                return weatherCondition.ToString();
            }
        }

        /// <summary>
        /// 是商铺
        /// </summary>
        /// <param name="strUserName"></param>
        /// <returns></returns>
        public Boolean isShop(Object UserKey)
        {
            Boolean bl = true;

            var User = new MembershipDataContext();
            var UserList = from u in User.aspnet_Users
                           from r in User.aspnet_Roles
                           from uir in User.aspnet_UsersInRoles
                           where u.UserId == (Guid)UserKey
                           where u.UserId == uir.UserId
                           where uir.RoleId == r.RoleId
                           where new string[] { "coporateVIP", "coporateNormal" }.Contains(r.RoleName)  
                           select u;

            if (UserList.ToList().Count == 0)
            {
                bl = false;
            }

            return bl;
        }
    }
}
